package com.mappn.backup.contact;

import static com.mappn.backup.contact.ContactProvider.CONTENT_URI_DATA_T;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class ContactTFileStore {

	private static final String TAG = "ContactFileStore";
	
	
	public static ContactTVo fromCursorSd(Cursor c){
		final ContactTVo contactTVoFromQuery = new ContactTVo();
		contactTVoFromQuery.set_id(c.getInt(c.getColumnIndex(ContactProvider.CONTACT_ID)));
		contactTVoFromQuery.setName(c.getString(c.getColumnIndex(ContactProvider.CONTACT_NAME)));
		contactTVoFromQuery.setPhone(c.getString(c.getColumnIndex(ContactProvider.CONTACT_PHONE)));
		contactTVoFromQuery.setEmail(c.getString(c.getColumnIndex(ContactProvider.CONTACT_ADDRESS)));
		contactTVoFromQuery.setAddress(c.getString(c.getColumnIndex(ContactProvider.CONTACT_EMAIL)));
		contactTVoFromQuery.setOrganizations(c.getString(c.getColumnIndex(ContactProvider.CONTACT_ORGANIZATION)));
		contactTVoFromQuery.setNote(c.getString(c.getColumnIndex(ContactProvider.CONTACT_NOTE)));
		contactTVoFromQuery.setNikename(c.getString(c.getColumnIndex(ContactProvider.CONTACT_NICKNAME)));
		return contactTVoFromQuery;
	}
	
	public static ContentValues getContentValuesPhone(ContactTVo contactTVo){
		final ContentValues values = new ContentValues();
		// 这里绝对不可以写get_id，这是系统自动生成的
		values.put(ContactProvider.CONTACT_NAME, contactTVo.getName());
		values.put(ContactProvider.CONTACT_PHONE, contactTVo.getPhone());
		values.put(ContactProvider.CONTACT_ADDRESS, contactTVo.getEmail());
		values.put(ContactProvider.CONTACT_EMAIL, contactTVo.getAddress());
		values.put(ContactProvider.CONTACT_ORGANIZATION, contactTVo.getOrganizations());
		values.put(ContactProvider.CONTACT_NOTE, contactTVo.getNote());
		values.put(ContactProvider.CONTACT_NICKNAME, contactTVo.getNikename());
		return values;
	}
	
	public static List<ContactTVo> queryAllFromSd(Context context){
		final ContentResolver cr = context.getContentResolver();
		List<ContactTVo> contactFiles = new ArrayList<ContactTVo>();
		ContactTVo contactTVo = null;
		Cursor c = cr.query(CONTENT_URI_DATA_T, null, null, null, null);
		if(c != null){
			if(c.getCount() > 0 && c.moveToFirst()){
				do{
					contactTVo = fromCursorSd(c);
					contactFiles.add(contactTVo);
				} while(c.moveToNext());
			}
			c.close();
		}
		return contactFiles;
	}
	
	
//	public static boolean whetherHaveData(Context context){
//		final ContentResolver cr = context.getContentResolver();
//		Cursor c = cr.query(CONTENT_URI_DATA, null, null, null, null);
//		boolean tag = false;
//		if(c != null){
//			if(c.getCount() > 0){
//				tag = true;
//			}
//		}
//		return tag;
//	}
	
//	public static List<ContactVo> selectAllByName(Context context,String name , String level){
//		final ContentResolver cr = context.getContentResolver();
//		List<ContactVo> demoFiles = new ArrayList<ContactVo>();
//		ContactVo demoVo = null;
//		String selection = ContactProvider.COL_NAME + "=? and " + ContactProvider.COL_LEVEL + "=?";
//		String[] selectionArgs = new String[] {name,level};
//		Cursor c = cr.query(CONTENT_URI_CONTACT, null, selection, selectionArgs, null);
//		if(c != null){
//			if(c.getCount() > 0 && c.moveToFirst()){
//				do{
//					demoVo = fromCursor(c);
//					demoFiles.add(demoVo);
//				} while(c.moveToNext());
//			}
//			c.close();
//		}
//		return demoFiles;
//	}
	
	public static int querySdCounts(Context context){
		final ContentResolver cr = context.getContentResolver();
		Cursor c = cr.query(CONTENT_URI_DATA_T, null, null, null, null);
		int count = 0;
		if(c != null ){
//			if(c.getCount() > 0 && c.moveToFirst()){
//				count++;
//			}
			count = c.getCount();
			c.close();
		}
		return count;
		
	}
	
	public static void deleteSdAll(Context context){
		final ContentResolver cr = context.getContentResolver();
		cr.delete(CONTENT_URI_DATA_T, null, null);
	}
	
//	public static void updateByName(Context context , String name ,String level){
//		final ContentResolver cr = context.getContentResolver();
//		ContentValues values = new ContentValues();
//		values.put(ContactProvider.COL_LEVEL, level);
//		String selection = ContactProvider.COL_NAME + " =?";
//		cr.update(CONTENT_URI_CONTACT, values, selection, new String[]{name});
//	}
//	
//	public static void deleteByName(Context context , String name){
//		final ContentResolver cr = context.getContentResolver();
//		String selection = ContactProvider.COL_NAME + "=?";
//		cr.delete(CONTENT_URI_DATA, selection, new String[]{name});
//	}
	
	public static void insertToSd(Context context, ContactTVo contactTVo){
		final ContentResolver cr = context.getContentResolver();
		Log.d(TAG, "uri:"+CONTENT_URI_DATA_T);
		cr.insert(CONTENT_URI_DATA_T, getContentValuesPhone(contactTVo));
	}
//	
//	public static void insertToPhone(Context context, RawContactVo rawContactVo){
//		final ContentResolver cr = context.getContentResolver();
//		Log.d(TAG, "uri:"+CONTENT_URI_RAWCONTACT_PHONE);
//		cr.insert(CONTENT_URI_RAWCONTACT_PHONE, getContentValuesSD(rawContactVo));
//	}
}
